﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class TInventoryLogsRepository : BaseRepository<TInventoryLog>, ITInventoryLogsRepository
    {
        public TInventoryLogsRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }


        /// <summary>
        /// 查询库存流水
        /// </summary>
        /// <param name="WarehouseId"></param>
        /// <param name="AreaId"></param>
        /// <param name="SourceType"></param>
        /// <param name="MaterialId"></param>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<TInventoryLog> GetInventoryflow(out int totalCount , int? WarehouseId, int? AreaId, int? SourceType, string? MaterialName, 
            string? StartTime, string? EndTime,
            int pageIndex, int pageSize)
        {
            var list = Context.TInventoryLog.AsQueryable();

            if (WarehouseId.HasValue)
            {
                list = list.Where(x => x.WarehouseId  == WarehouseId);
            }
            if (AreaId.HasValue)
            {
                list = list.Where(x => x.AreaId == AreaId);
            }
            if (SourceType.HasValue)
            {
                list = list.Where(x => x.SourceType == SourceType);
            }
            if (MaterialName != null)
            {
                list = list.Where(x => x.MaterialName.Contains(MaterialName));
            }

            if (StartTime!=null && EndTime!=null)
            {
                string StartdateString = StartTime;
                DateTime Startdate = DateTime.Parse(StartdateString);
                string EnddateString = EndTime;
                DateTime Enddate = DateTime.Parse(EnddateString);
                list = list.Where(x => x.CreateDate >= Startdate && x.CreateDate <= Enddate);

            }

           

            totalCount = list.Count();

            list = list.OrderByDescending(x => x.CreateDate).Skip((pageIndex - 1) * pageSize).Take(pageSize);



            return list.ToList();
        }


        public List<TInventoryLog> GetAllInventoryLog()
        {
            return Context.TInventoryLog.ToList();
        }

    }
}
